class_name WeaponC_Pickable
extends WeaponC

@export var rise_duration:float = 0.1
@export var drop_duration:float = 0.3
@export var interactive_item: InteractiveItem
var _drop_tween:Tween
var _drop_end_pos:Vector2
var can_pick_up:bool = false:
	get:
		return can_pick_up and weapon and weapon.owner_entity == null

func init_drop_info(root_pos:Vector2):
	_drop_end_pos = root_pos
func show_tip(area):
	if can_pick_up:
		var entity = Util.get_entity_in_parent(area)
		if entity == GamePlayerSystem.get_player():
			interactive_item.init_interactive_item(KEY_E,weapon.sprite2d)
func hide_tip(area):
	if can_pick_up:
		pass
func init_component(weapon:Weapon):
	super.init_component(weapon)
	interactive_item.set_enable(can_pick_up)
	interactive_item.interactive_btn.interact_completed.connect(func (entity):
		var weapon_component:WeaponComponent = entity.get_component_by_name("WeaponComponent") as WeaponComponent
		if weapon_component:
			weapon_component.obtain_weapon(weapon)
		)
func equip():
	super.equip()
	weapon.position = Vector2.ZERO
	weapon.global_rotation = 0
	weapon.scale = Vector2(abs(weapon.scale.x),abs(weapon.scale.y))
	interactive_item.set_enable(false)
	weapon.sprite2d.material.set_shader_parameter("enable",false)
func unequip():
	super.unequip()
func discard():
	if _drop_tween and _drop_tween.is_valid():
		_drop_tween.kill()
	var weapon_scale = Vector2.ONE
	var rot = fmod(weapon.global_rotation,TAU)
	if rot < 0:
		rot += TAU
	if rot >= PI/2 and rot <= PI * 3 / 2:
		weapon_scale = Vector2(1,-1)
		rot = PI
	else:
		weapon_scale = Vector2.ONE
		rot = 0
	weapon.global_rotation = rot
	weapon.apply_scale(weapon_scale)
	_drop_tween = create_tween()
	_drop_tween.tween_property(weapon,"global_position",Vector2(weapon.global_position.x,weapon.global_position.y - 12),rise_duration).set_ease(Tween.EASE_OUT)
	_drop_tween.tween_property(weapon,"global_position",Vector2(weapon.global_position.x,_drop_end_pos.y),drop_duration).set_ease(Tween.EASE_IN_OUT)
	_drop_tween.tween_callback(func ():
		can_pick_up = true
		interactive_item.set_enable(true)
		)
func _init() -> void:
	weapon_component_name = "WeaponC_Pickable"
func _ready() -> void:
	interactive_item.set_enable(false)
	interactive_item.area_entered.connect(show_tip)
	interactive_item.area_exited.connect(hide_tip)
